################################################################################ 
#
# Counterspeech encouraging users to adopt the perspective of minority 
# groups reduces hate speech and its amplification on social media
#
# Perspective-centered treatments vs Disapproval
#
################################################################################ 


rm(list = ls())

# Set to save results
setsave = T

################################################################################ 
#  LIBRARIES
################################################################################ 

library(hdm)         # For rlassoEffect()
library(dplyr)       # For data manipulation
library(readr)       # For read_rds()
library(ggplot2)     # Optional (for plotting, if you activate that section)
library(xtable)      # Used to print model summaries in LaTeX table format
library(lubridate)   # Only needed if you're working with date-time variables

set.seed(123)

################################################################################ 
#   DATA AND FOLDER
################################################################################ 

wd = ''
wd_res = paste0(wd, '/results')
wd_data =  paste0(wd, '/data')


# Upload the data
data = read.csv(paste0(wd_data, '/dataset_analysis.csv'))
data = data[!(data$user_deleted == "yes"),]

temp = readRDS(paste0(wd_data, '/users_annotated_replies.rds'))
temp$negative_reply = ifelse(temp$restating_backlash_hate==1 | temp$attack==1, 1, 0)
temp$reply = ifelse(temp$reply_count>0, 1, 0)
temp = temp[c("user_id", "negative_reply", "reply")]
temp[c('reply', 'negative_reply')][is.na(temp[c('reply', 'negative_reply')])] = 0

data = merge(data, temp, by="user_id", all.x = T)


lassodata = read_rds(paste0(wd_data, '/data_lasso.RDS'))
lassodata = lassodata[-1]

# Fix reply data 



################################################################################ 
#   Prepare
################################################################################ 


outvars = c('orig_tweet_deleted', 'total_tweets_post',
            'tox_dummy_p_key_num', 'tox_dummy_p_key',
            'n_favorites_to_orig_tweet', 'n_retweets_to_orig_tweet', 
            "reply", "negative_reply")

outvars_name = c('Xenophobic Tweet Deleted', '# Total Tweets',
                 '# Xenophobic Tweets', 'Xenophobic Tweet Share',
                 'No. of likes received\nby xenophobic tweet', 'No. of retweets\nof xenophobic tweet',
                 "Replied", "Backlash")

treatments = c('empathy', 'traditional', 'analogical', 'perspective_getting')
treatments_lab = c( 'Empathy', 'Traditional Perspective Taking',  'Analogical Perspective Taking',
                    'Perspective Getting')


################################################################################ 
#   Scale variables
################################################################################ 


data[outvars] = lapply(data[outvars], scale)
lassodata = lapply(lassodata, as.numeric)
lassodata = lapply(lassodata, scale)
lassodata = as.data.frame(lassodata)

################################################################################ 
#   Run the reg with double lasso selection
################################################################################ 


MODELS = list()

n = 1

for (j in 1:length(outvars)){
  for (i in 1:length(treatments)){
    
    # drop missings
    droper = union(which(is.na(data[outvars[j]])), which(data[outvars[j]]==Inf))
    
    # keep the right treatment and control group
    keeper = union(which(data$disapproval == 1),  which(data[treatments[i]] == 1))
    keeper = setdiff(keeper, droper)
    
    
    y = data[keeper, outvars[j]]
    d = data[,treatments[i]][keeper]
    x = lassodata[keeper, ] %>% as.matrix()
    
    c = rep(FALSE, ncol(x))
    c[c(2,3)] = TRUE
    
    MODELS[[n]] = rlassoEffect(x, y, d, method = "double selection")
    
    print('Done with: ')
    print(n)
    
    n = n+1
    
  }}







################################################################################ 
#   Save results in a container
################################################################################ 


MOD = MODELS

coef = sapply(MOD, function(m) summary(m)[[1]][1])
se = sapply(MOD, function(m) summary(m)[[1]][2])
pval = sapply(MOD, function(m) summary(m)[[1]][4])
regnum =  sapply(MOD, function(m) length(m$coefficients.reg)-2)
sample = sapply(MOD, function(m) m$samplesize)
controls = sapply(MOD, function(m) paste0(names(m$coefficients.reg)[-c(1, 2)], collapse = ', '))

container = cbind.data.frame(yvar = rep(outvars, each=length(treatments)),
                             yvar_name = rep(outvars_name, each=length(treatments)),
                             treat = rep(treatments, time=length(outvars)),
                             treatments_lab = rep(treatments_lab, time=length(outvars)),
                             coef = coef, se=se, pval=pval,
                             samplesize = sample,
                             lasso_cont_num = regnum,
                             controls = controls
)




if (setsave){write.csv(container, paste0(wd_res, '/tables/container_lasso_disapproval_controls.csv'), row.names = F)}
